#include "beep.h"

void Beep_init(void)
{
	uint16_t CCR1Val = 249;
	uint16_t PrescalerValue = 0;

	PrescalerValue = (uint16_t) (SystemCoreClock / 24000000) - 1;

	TMR_TimerBaseInitType  TMR_TMReBaseStructure;
	TMR_OCInitType  TMR_OCInitStructure;
	GPIO_InitType GPIO_InitStructure;

  /* Enable TMR10 and GPIOF clock */
  RCC_APB2PeriphClockCmd(RCC_APB2PERIPH_TMR10 | RCC_APB2PERIPH_GPIOB, ENABLE);

  /* GPIOF Configuration: TMR10 Channel1 as alternate function push-pull */
  GPIO_InitStructure.GPIO_Pins = GPIO_Pins_8;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  GPIO_InitStructure.GPIO_MaxSpeed = GPIO_MaxSpeed_50MHz;

  GPIO_Init(GPIOB, &GPIO_InitStructure);
   TMR_TimeBaseStructInit(&TMR_TMReBaseStructure);
  TMR_TMReBaseStructure.TMR_Period = 665;
  TMR_TMReBaseStructure.TMR_DIV = PrescalerValue;
  TMR_TMReBaseStructure.TMR_ClockDivision = 0;
  TMR_TMReBaseStructure.TMR_CounterMode = TMR_CounterDIR_Up;

  TMR_TimeBaseInit(TMR10, &TMR_TMReBaseStructure);

  /* PWM1 Mode configuration: Channel1 */
  TMR_OCStructInit(&TMR_OCInitStructure);
  TMR_OCInitStructure.TMR_OCMode = TMR_OCMode_PWM1;
  TMR_OCInitStructure.TMR_OutputState = TMR_OutputState_Enable;
  TMR_OCInitStructure.TMR_Pulse = CCR1Val;
  TMR_OCInitStructure.TMR_OCPolarity = TMR_OCPolarity_High;

  TMR_OC1Init(TMR10, &TMR_OCInitStructure);

  TMR_OC1PreloadConfig(TMR10, TMR_OCPreload_Enable);

  TMR_ARPreloadConfig(TMR10, ENABLE);

  /* TMR10 enable counter */
  TMR_Cmd(TMR10, ENABLE);
  TMR_CtrlPWMOutputs(TMR10,DISABLE);
}

void Beep_On(void)
{
	TMR_CtrlPWMOutputs(TMR10,ENABLE);
}

void Beep_Off(void)
{
	TMR_CtrlPWMOutputs(TMR10,DISABLE);
}
